package com.earnings.okhttputils.utils.adapter;

import android.content.Context;
import android.databinding.ViewDataBinding;
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.zhy.autolayout.utils.AutoUtils;

/**
 * Created by yy on 2015/5/10.
 */
public class DataBindingViewHolder<T extends ViewDataBinding> extends RecyclerView.ViewHolder {
    private SparseArray<View> mViews;
    private int mPosition;
    private View mConvertView;
    private Context mcontext;
    private int mCount;
    private int mType;
    private T mBinding;

    public DataBindingViewHolder(Context context, T binding, int Type) {
        super(binding.getRoot());
        AutoUtils.autoSize(binding.getRoot());
        mBinding = binding;
        mConvertView = itemView;
        mcontext = context;
        mType = Type;
        this.mViews = new SparseArray<View>();
        mConvertView.setTag(this);
    }
    public T getBinding(){
        return mBinding;
    }
    public int getmType() {
        return mType;
    }

    public void setmType(int mType) {
        this.mType = mType;
    }
    //
//    private  ViewHolder(Context context, ViewGroup parent, int layoutid, int position, int count) {
//
//        this.mPosition = position;
//        this.mConvertView = LayoutInflater.from(context).inflate(layoutid, parent, false);
//        this.mViews = new SparseArray<View>();
//        mcontext = context;
//        mCount = count;
//        mConvertView.setTag(this);
//
//    }

    public static DataBindingViewHolder get(Context context, ViewDataBinding convertView, int Type) {
        return new DataBindingViewHolder(context, convertView, Type);
    }

    public DataBindingViewHolder(Context context, View itemView) {
        super(itemView);
        mcontext = context;
        mConvertView = itemView;
        mViews = new SparseArray<View>();
    }

    public static DataBindingViewHolder createViewHolder(Context context, View itemView) {
        DataBindingViewHolder holder = new DataBindingViewHolder(context, itemView);
        return holder;
    }

    public static DataBindingViewHolder createViewHolder(Context context, ViewGroup parent, int layoutId) {
        View itemView = LayoutInflater.from(context).inflate(layoutId, parent,
                false);
        DataBindingViewHolder holder = new DataBindingViewHolder(context, itemView);
        return holder;
    }


    /*
    * 通过viewid得到控件
    *
    * */
    public <T extends View> T getView(int viewid) {
        View view = mViews.get(viewid);
        if (view == null) {
            view = mConvertView.findViewById(viewid);
            mViews.put(viewid, view);
        }
        return (T) view;
    }

    public int getmCount() {
        return mCount;
    }

    public void setmCount(int mCount) {
        this.mCount = mCount;
    }

    public View getConvertView() {

        return mConvertView;
    }


    public DataBindingViewHolder setText(int viewId, String text) {
        TextView view = getView(viewId);
        view.setText(text);
        return this;

    }

    public DataBindingViewHolder setImageResId(int viewId, int resid) {

        ImageView view = getView(viewId);
        view.setImageResource(resid);
        return this;
    }

    public DataBindingViewHolder setImageBitmap(int viewId, Bitmap bitmap) {
        ImageView view = getView(viewId);
        view.setImageBitmap(bitmap);
        return this;
    }


}
